
#include "Plateau.h"


void initPlateau(Plateau *pPlat, const int nbrJoueur)
{
   int i;
   pPlat->tab = malloc (36*sizeof(Case*));
   for (i=0; i<36;i++)
   {
       pPlat->tab[i] = creerCase(nbrJoueur);
   }
}

Plateau * creerPlateau(const int nbrJoueur)
{
    Plateau *pPlatNew = (Plateau *) malloc(sizeof(Plateau));
    initPlateau(pPlatNew,nbrJoueur);
    return pPlatNew;
}

void liberePlateau(Plateau *pPlat)
{
    int i;
    for (i=0;i<36;i++)
    {
        detruitCase(&pPlat->tab[i]);
    }
    free(pPlat->tab);
    pPlat->tab = NULL;
}

void detruitPlateau(Plateau **ppPlat)
{
    liberePlateau(*ppPlat);
    free(*ppPlat);
    *ppPlat = NULL;
}


int get_poscase(const Plateau *pPlat,const Case * pCase)
{
    int i=0;
  for (i=0;i<=35;i++)
  {
      if(pPlat->tab[i] == pCase)
      return i;
  }
  return i;
}

void set_case(Plateau *pPlat,int num_case,Case * pCase)
{
    assert((num_case>=0) && (num_case<=35));
    pPlat->tab[num_case]=pCase;
}

Case * get_case(const Plateau *pPlat, int num_case)
{
    assert ((num_case>=0) && (num_case<=35));
    return pPlat->tab[num_case];
}

void repartirCase(Plateau *pPlat)
{
    int i;
    int j;
    GenreCase g;
    const char tab[34]="EEEPEEBEEEPEEBEEEPEEBEEEPEEBEEEPEE";
    setGenreCase(pPlat->tab[0],rien);
    setGenreCase(pPlat->tab[35],rien);
    for(i=1;i<35;i++)
    {
        j=i-1;
        if(tab[j] =='E')
            g = exam;
        else if(tab[j]=='B')
            g = bonus;
        else if(tab[j]=='P')
            g = pioche;
        else
            g = rien;

        setGenreCase(pPlat->tab[i],g);

    }

}

bool verifPosition(const int taille, const int positionf)
{
    if(taille == positionf)
        return 2;
    else if(positionf > taille)
        return 1;
    else
        return 0;
}

bool verifCredit(const int total, const int score)
{
    if(score >= total)
        return 1;
    else
        return 0;
}

void repartirQuest(Plateau * pla, ListeTempQ * liste)
{
    int i;
    int nbrcases = 35;
    int nbrquest = nbrElementListeQ(liste);
    int nbralea;

    for(i=1;i<nbrcases;i++)
    {
        if(pla->tab[i]->genreCase == exam)
        {
            nbralea = rand()%(nbrquest);
            setActionCase(pla->tab[i], liste->tab[nbralea]);
            repartirReponseAlea(pla->tab[i]->actionCase);
            supprimerElementListe(liste,nbralea);
            nbrquest = nbrElementListeQ(liste);
        }
    }
}
